<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
  import echarts from 'echarts';

  require('echarts/theme/macarons');
  import resize from '@/views/dashboard/mixins/resize';
  import { getProjectHundredChaptersAccumulateChartBar } from "@/api/engineering/summary";

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '450px'
      },
      projectId: {
        type: Number,
        required: true
      }
    },
    data() {
      return {
        chart: null,
      }
    },
    watch: {
      projectId: {
        deep: true,
        handler(val) {
          this.projectId = val;
          this.setOptions();
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart();
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.setOptions();
      },
      setOptions() {
        console.log("BarChart-----项目ID：" + this.projectId);
        // 加载数据
        getProjectHundredChaptersAccumulateChartBar(this.projectId).then(response => {
          this.chart = echarts.init(this.$el, 'macarons');
          let option = {
            title: {
              text: '百章完成金额与合同金额统计',
              subtext: '单位（万元）',
              left: 'center'
            },
            tooltip: {
              trigger: 'item'
            },
            grid: {
              top: 80,
              show: false
            },
            legend: {
              top: 50,
              data: ['合同', '完成']
            },
            yAxis: {
              name: '金额',
              nameTextStyle: {
                color: '#fff'
              },
              type: 'value'
            },
            xAxis: [{
                data: response.axis
              }, {
                type: 'category',
                axisLine: {show: false},
                axisTick: {show: false},
                axisLabel: {show: false},
                splitArea: {show: false},
                splitLine: {show: false},
                data: response.axis
              }, {
                type: 'category',
                axisLine: {show: false},
                axisTick: {show: false},
                axisLabel: {show: false},
                splitArea: {show: false},
                splitLine: {show: false},
                data: response.axis
              },
            ],
            series: [{
                name: '合同',
                type: 'bar',
                barGap: '0',
                // barWidth: '90%',
                // itemStyle: {normal: {color: '#3A73A9', label: {show: false, textStyle: {color: '#fff'}}}},
                data: response.contractTotal
              }, {
                name: '完成',
                type: 'bar',
                barGap: '0',
                // barWidth: '75%',
                xAxisIndex: 2,
                itemStyle: {
                  normal: {
                    color: '#84c66e',
                    label: {show: false, textStyle: {color: '#27727B'}}
                  }
                },
              data: response.finishTotal
              }
            ]
          }
          this.chart.setOption(option);
          if (response.axis.length == 0) {
            this.chart.showLoading({
              text: '暂无数据',
              showSpinner: false,
              textColor: '#808080',
              maskColor: '#0f1325',
              fontSize: '26px',
              fontWeight: 'bold'
            })
          } else {
            this.chart.hideLoading();
          }
        });
      }
    }
  }

</script>
